﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Events;
using JBeamLibrary.VehicleParts.Elements;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Collections
{
    public class ThrusterCollection : Collection<Thruster>, IVehicleGroupCollection<Thruster>
    {
        #region Fields

        public event ElementPropertyChangedEventHandler ElementPropertyChanged;

        #endregion

        #region Indexers

        #endregion

        #region CTORs

        /// <summary>Creates a new beams collection</summary>
        public ThrusterCollection() { }

        /// <summary>Creates a new beams collection</summary>
        /// <param name="thrusters"></param>
        public ThrusterCollection(IEnumerable<Thruster> thrusters)
        {
            AddRange(thrusters);
        }

        #endregion

        #region Methods

        public new void Add(Thruster thruster)
        {
            base.Add(thruster);

            thruster.PropertyChanged += (s, e) =>
            {
                if (ElementPropertyChanged != null)
                    ElementPropertyChanged(s, e);
            };
        }

        public void Add(Node node1, Node node2)
        {
            Add(node1.Id, node2.Id);
        }

        public void Add(string nodeId1, string nodeId2)
        {
            Add(new Thruster(nodeId1, nodeId2));
        }

        public void AddRange(IEnumerable<Thruster> thrusters)
        {
            foreach (var thruster in thrusters)
                Add(thruster);
        }

        public void Remove(Node node1, Node node2)
        {
            Remove(node1.Id, node2.Id);
        }

        public void Remove(string nodeId1, string nodeId2)
        {
            for (var i = 0; i < Count; i++)
            {
                var thruster = this[i];
                if (!thruster.NodeId1.Equals(nodeId1) || !thruster.NodeId2.Equals(nodeId2))
                    continue;

                RemoveAt(i);
                i--;
            }
        }

        public new void Remove(Thruster element)
        {
            base.Remove(element);
        }

        #endregion
    }
}
